import React from 'react';
import {Layout, List, Modal} from 'antd';
import './index.less';
import {connect} from "dva";
import BaseComponent from "components/Base/BaseComponent";
import Button from "antd/lib/button";
import moment from "moment";

const {Content, Footer, Header} = Layout;

@connect(({AutoEcg}) => ({AutoEcg}))
export default class extends BaseComponent {
  state = {
    visible: false,
    AutoEcg: [],
    ecg: 'AutoEcg',
    open:false
  };

  AutoEcg = () => {
    clearInterval(this.timer);
    this.setState({AutoEcg: [], ecg: 'AutoEcg'});
    this.time();
  };
  AutoFilterEcg = () => {
    clearInterval(this.timer);
    this.setState({AutoEcg: [], ecg: 'AutoFilterEcg'});
    this.time();
  };

  getNamespace() {
    return 'AutoEcg';
  }
  showAutoEcg = () => {
    this.setState({visible: true, patientId: this.props.patientId});
    this.stop();
  };
  handleCancel = () => {
    this.stop();
    this.setState({visible: false, AutoEcg: [],open:false});
  };
  stop = () => {
    clearInterval(this.timer);
  };
  go = () => {
    this.refresh();
    this.time();
  };
  time=()=>{
    this.timer = setInterval(() => {
      this.refresh();
    }, 500)
  };
  refresh = () => {
    let token = '';
    if (this.state.ecg == 'AutoEcg') {
      let AutoEcgData=this.props.AutoEcg.AutoEcg;
      let length = AutoEcgData.length;
      if (this.state.open) {
        if (length > 0) {
          token = moment(AutoEcgData[length-1].startTime, 'YYYY-MM-DD HH:mm:ss SSS').valueOf();
        }
      }
      this.dispatch({
        type: `${this.getNamespace()}/AutoEcg`,
        payload: {
          limit: '4',
          patientId: this.props.patientId,
          token: token
        }
      });
      let Ecg = [];
      let AutoEcg = Ecg.concat(AutoEcgData, this.state.AutoEcg);
      this.setState({AutoEcg: AutoEcg,open:true});
    } else {
      let AutoFilterEcgData=this.props.AutoEcg.AutoFilterEcg;
      let length = AutoFilterEcgData.length;
      if (this.state.open) {
        if (length > 0) {
          token = moment(AutoFilterEcgData[length-1].startTime, 'YYYY-MM-DD HH:mm:ss SSS').valueOf();
        }
      }
      this.dispatch({
        type: `${this.getNamespace()}/AutoFilterEcg`,
        payload: {
          limit: '4',
          patientId: this.props.patientId,
          token: token
        }
      });
      let Ecg = [];
      let AutoFilterEcg = Ecg.concat(AutoFilterEcgData.reverse(), this.state.AutoEcg);
      this.setState({AutoEcg: AutoFilterEcg,open:true});
    }
  }
  render() {
    const {visible, patientId} = this.state;
    return (
      <a>
        <a onClick={this.showAutoEcg}>
          <span>测试心电</span>
        </a>
        <Modal
          title="心电列表"
          visible={visible}
          // onOk={this.handleOk}
          onCancel={this.handleCancel}
          footer={null}
          className='autoecg'
          bodyStyle={{overflowY: "auto", maxHeight: 500}}
        >
          <Button onClick={this.AutoEcg}>原始心电</Button>
          <Button onClick={this.AutoFilterEcg}>滤波心电</Button>
          <Button type="danger" onClick={this.stop}>暂停</Button>
          <Button type="primary" onClick={this.go}>继续</Button>
          <List
            size="small"
            bordered
            dataSource={this.state.AutoEcg}
            renderItem={item => <List.Item>{item.startTime}</List.Item>}
          />
        </Modal>
      </a>
    );
  }

}
